package com.stephen.redisdemo.thread;

/**
 * @author: stephen
 * @create: 2020-03-01 21:57
 * @description: 死锁
 */
class HoldLockThread implements Runnable {
    private String lockA;
    private String lockB;

    public HoldLockThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA) {
            System.out.println(Thread.currentThread().getName() + "\t持有" + lockA + "\t尝试获得" + lockB);
            synchronized (lockB) {

            }
        }
    }
}

public class DeadLockDemo {

    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "lockB";
        new Thread(new HoldLockThread(lockA, lockB),"t1").start();
        new Thread(new HoldLockThread(lockB, lockA),"t2").start();

    }

}
